<?php
// +----------------------------------------------------------------------
// | Genadmin [ Genadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\setting;

use app\admin\logic\system\SystemConfigGroupLogic;
use app\admin\logic\system\SystemConfigLogic;
use app\admin\validate\system\SystemConfigValidate;
use app\basic\BaseController;
use support\Request;
use support\Response;
use support\utils\Cache;

/**
 * 配置项数据控制器
 */
class SystemConfigController extends BaseController
{
    /**
     * 构造
     */
    public function __construct()
    {
        $this->logic = new SystemConfigLogic();
        $this->validate = new SystemConfigValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request) : Response
    {
        $where = $request->more([
            ['group_id', ''],
            ['name', ''],
            ['key', ''],
        ]);
        $query = $this->logic->search($where);
        $data = $this->logic->getAll($query);
        return $this->success($data);
    }

    /**
     * 修改配置内容
     * @param Request $request
     * @return Response
     */
    public function batchUpdate(Request $request): Response
    {
        $group_id = $request->post('group_id');
        $config = $request->post('config');
        $groupLogic = new SystemConfigGroupLogic();
        $group = $groupLogic->where('id', $group_id)->findOrEmpty();
        if ($group->isEmpty()) {
            $this->fail('配置分组查找失败');
        }
        $saveData = [];
        foreach ($config as $key => $value) {
            $saveData[] = [
                'id' => $value['id'],
                'sort' => $value['sort'],
                'name' => $value['name'],
                'key' => $value['key'],
                'value' => $value['value']
            ];
        }
        $this->logic->saveAll($saveData);
        Cache::clear('cfg_'.$group->code);
        return $this->success('操作成功');
    }

    /**
     * 修改单项配置信息
     * @param Request $request
     * @return Response
     */
    public function setConfigUpdate(Request $request): Response
    {
        $config = $request->post();
        $key = $config['key'];
        $name = $config['name'];
        $configValues = $this->logic->where("key",$key)->find();
        unset($config['name'],$config['key']);
        $values = $config;
        $saveData = [
            'sort' => '100',
            'group_id' => 0,
            'name' => $name,
            'key' => $key,
            'value' => json_encode($values,JSON_UNESCAPED_UNICODE),
            'input_type' =>'array'
        ];
        if(!empty($configValues)){
            // 清空缓存
            $prefix = 'config_';
            Cache::clear($prefix.$key);
            $this->logic->where(['id'=>$configValues['id']])->update($saveData);
        }else{
            $this->logic->save($saveData);
        }
        return $this->success('操作成功');
    }

    /**
     * 获取单项配置信息
     * @param Request $request
     * @return Response
     */
    public function getConfigValues(Request $request)
    {
        $key = $request->get("key");
        $configValues = $this->logic->getConfig($key)['value'];
        return $this->success($configValues);
    }
}
